package com.lehuowujie.lehuomarket.api;

import android.util.Log;

import com.lehuowujie.lehuomarket.util.Constant;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/11/23.
 */
public class APIUtil {
    private static RetrofitAPI api;

    public static RetrofitAPI getApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.URL_BASE)
                .client(genericClient(null))
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(new FileRequestBodyConverterFactory())
                .build();
        return retrofit.create(RetrofitAPI.class);
    }

    public static RetrofitAPI getApi(String token) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.URL_BASE)
                .client(genericClient(token))
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(new FileRequestBodyConverterFactory())
                .build();
        return retrofit.create(RetrofitAPI.class);
    }

    static class FileRequestBodyConverterFactory extends Converter.Factory {
        @Override
        public Converter<File, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            return new FileRequestBodyConverter();
        }
    }

    static class FileRequestBodyConverter implements Converter<File, RequestBody> {

        @Override
        public RequestBody convert(File file) throws IOException {
            return RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        }
    }

    //; charset=UTF-8
    public static OkHttpClient genericClient(final String token) {

        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = null;
                        Request.Builder builder = chain.request()
                                .newBuilder()
                                .addHeader("Content-Type", "application/json; charset=UTF-8");
                        if (token == null || "".equals(token)) {
                            request = builder.build();
                        } else {
                            Log.e("token", "token" + token);
                            request = builder.addHeader("Authorization", "Token " + token)
                                    .build();
                        }
                        return chain.proceed(request);
                    }
                })
                .build();

        return httpClient;
    }

}
